package com.competition.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.competition.commonutils.R;
import com.competition.servicebase.exceptionhandler.MyException;
import com.competition.vod.Utils.ConstantVodUtils;
import com.competition.vod.Utils.InitVodClient;
import com.competition.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("video")
@Api(description = "视频上传的接口，只负责上传与删除视频，并无操作数据库的功能")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传到阿里云点播
    @PostMapping("uploadAlyVideo")
    @ApiOperation("上传视频，返回视频id,可通过视频id获取视频播放凭证")
    public R uploadAlyVideo(@RequestParam("file") MultipartFile file) {
        String videoId = vodService.uploadVideoAly(file);
        String originalFilename = file.getOriginalFilename();
        return R.ok().data("videoId", videoId).data("originalFilename",originalFilename);
    }

    //根据视频id删除视频
    @DeleteMapping("removeAlyVideo/{id}")
    @ApiOperation("根据视频id删除视频")
    public R removeAlyVideo(@PathVariable String id) {
        try {
            //初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
            //向request对象设置id
            deleteVideoRequest.setVideoIds(id);
            //调用初始化方法删除视频id
            defaultAcsClient.getAcsResponse(deleteVideoRequest);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001, "删除视频失败");
        }
    }

    //根据课程id删除多个视频(参数是多个视频id)
    @DeleteMapping("deleteBathVideo")
    public R deleteBathVideoById(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        for (String id : videoIdList) {
            System.out.println(id);
        }
        return R.ok();
    }

    //根据视频id获取视频凭证
    @GetMapping("getPlayAuth/{id}")
    @ApiOperation("根据视频id获取视频凭证")
    public R getPlayAuth(@PathVariable String id) {
        System.out.println(id);
        try {
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(id);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth).data("id",id);
        }catch(Exception e) {
            throw new MyException(20001,"获取凭证失败");
        }
    }
}
